package com.ds.dsm.view.config.form.service;

import com.ds.common.JDSException;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.dsm.view.config.form.field.FieldFormBean;
import com.ds.dsm.view.config.form.field.group.*;
import com.ds.dsm.view.config.form.field.list.FieldCheckBoxGroup;
import com.ds.dsm.view.config.form.field.list.FieldListGroup;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.form.CustomFormViewBean;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavGroupViewAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.dsm.view.field.FieldFormConfig;
import net.sf.cglib.beans.BeanMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping(path = "/dsm/view/config/form/field/group/")
@MethodChinaName(cname = "字段配置", imageClass = "spafont spa-icon-c-comboinput")

public class FormFieldService {


    @MethodChinaName(cname = "清空配置信息")
    @RequestMapping(method = RequestMethod.POST, value = "clearFieldForm")
    @APIEventAnnotation(callback = {CustomCallBack.Reload})
    public @ResponseBody
    ResultModel<Boolean> clearFieldForm(String serviceClassName, String methodName, String fieldname, String domainId, String viewInstId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            if (serviceClassName != null && !serviceClassName.equals("")) {
                ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(serviceClassName, viewInstId);
                MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
                CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
                formViewBean.getFieldConfigMap().remove(fieldname);
                DSMFactory.getInstance().getViewManager().reSetViewEntityConfig(serviceClassName, domainId, viewInstId);
            }
        } catch (JDSException e) {
            ErrorResultModel<Boolean> errorResult = new ErrorResultModel<Boolean>();
            errorResult.setErrdes(e.getMessage());
            result = errorResult;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return result;
    }


    @MethodChinaName(cname = "编辑字段配置信息")
    @RequestMapping(method = RequestMethod.POST, value = "updateFieldForm")
    @APIEventAnnotation(callback = {CustomCallBack.Reload}, bindMenu = CustomMenuItem.save)
    public @ResponseBody
    ResultModel<Boolean> updateFieldForm(@RequestBody FieldFormBean config) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            String className = config.getSourceClassName();
            if (className != null && !className.equals("")) {
                ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(className, config.getViewInstId());
                MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(config.getSourceMethodName());
                CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
                FieldFormConfig fieldConfig = (FieldFormConfig) formViewBean.getFieldConfigMap().get(config.getFieldname());
                Map<String, Object> configMap = BeanMap.create(fieldConfig);
                configMap.putAll(BeanMap.create(config));
                Map<String, Object> widgetMap = BeanMap.create(fieldConfig.getWidgetConfig());
                widgetMap.putAll(BeanMap.create(config));
                DSMFactory.getInstance().getViewManager().updateViewEntityConfig(tableConfig);
            }
        } catch (JDSException e) {
            ErrorResultModel<Boolean> errorResult = new ErrorResultModel<Boolean>();
            errorResult.setErrdes(e.getMessage());
            result = errorResult;
        }

        return result;
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldNav")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "字段信息")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldNav(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldAudio")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "音频")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldAudioGroup> getFieldAudio(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldButton")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "复合框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldButtonGroup> getFieldButton(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldCheckBox")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "复选框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldCheckBoxGroup> getFieldCheckBox(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldCodeEditor")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "代码编辑器")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldCodeEditorGroup> getFieldCodeEditor(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldComboBox")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "复合输入")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFielComboBox(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldFlash")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "Flash")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldFlashGroup> getFieldFlash(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldIcon")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "图标")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldIconGroup> getFieldIcon(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldInput")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "普通输入框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldInputGroup> getFieldInput(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldLabel")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "标签")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldLabelGroup> getFieldLabel(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldList")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "多选框")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldListGroup> getFieldList(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldNumber")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "数字输入")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldNumberGroup> getFieldNumber(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldProgressBar")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "进度条")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldProgressBarGroup> getFieldProgressBar(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldSlider")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "Slider滚动输入")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldSliderGroup> getFieldSlider(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldSVGPager")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "SVGPager")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldSVGPagerGroup> getFieldSVGPager(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldTextEditor")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "文本编辑器")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldComboGroup> getFieldTextEditor(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }

    @RequestMapping(method = RequestMethod.POST, value = "FieldVideo")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "视频")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldVideoGroup> getFieldVideo(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldModule")
    @NavGroupViewAnnotation(autoSave = true)
    @ModuleAnnotation(caption = "子模块信息")
    @APIEventAnnotation(bindMenu = {CustomMenuItem.editor, CustomMenuItem.add})
    @ResponseBody
    public ResultModel<FieldModuleGroup> getFieldModule(String sourceClassName, String projectId, String fieldname) {
        return new ResultModel<>();
    }
}
